/*!
\copyright  Copyright (c) 2024 - 2025 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      A2DP audio latency related management
*/

#include "aptx_ad_mode_monitor.h"
#include "audio_driver.h"
#include "audio_driver_event.h"
#include "audio_sources_list.h"
#include "kymera_config.h"

#include <audio_stack_events.h>
#include <message.h>
#include <logging.h>

static struct
{
    audio_driver_uc_instance_uid_t use_case;
    aptx_ad_mode_monitor_setup_t params;
    unsigned is_enabled:1;    
} state;

static const aptx_ad_mode_monitor_observer_if_t *registered_observer;

static void aptxAdModeMonitor_HandleR21Specific(uint16 ssrc)
{
    uint32_t kick_period_ms = 10; /* Default kick period for HQ/Lossless */
    if (ssrc == aptxAdaptiveLowLatencyStreamId_SSRC_Q2Q())
    {
        kick_period_ms = 2;
    }
    AudioDriver_SetKickPeriod(state.use_case, kick_period_ms);
}

static void aptxAdModeMonitor_AudioStackEventCallback(audio_stack_event_t event, key_value_list_t params)
{
    event_ssrc_value_t * ssrc = KeyValueList_GetSized(params, EVENT_SSRC_VALUE, sizeof(uint16_t));
    PanicNull(ssrc);
    DEBUG_LOG("aptxAdModeMonitor_AudioStackEventCallback event=0x%08x ssrc=0x%x", event, *ssrc);
    if (state.is_enabled)
    {
        if (state.params.is_q2q && registered_observer && registered_observer->SsrcUpdateInd)
        {
            registered_observer->SsrcUpdateInd(&state.params, *ssrc);
        }
        if (state.params.version == aptx_ad_mode_monitor_version_r21)
        {
            aptxAdModeMonitor_HandleR21Specific(*ssrc);
        }
    }
}

static void aptxAdModeMonitor_ResetState(void)
{
    memset(&state, 0, sizeof(state));
}

void AptxAdModeMonitor_RegisterObserver(const aptx_ad_mode_monitor_observer_if_t *observer)
{
    PanicFalse(registered_observer == NULL);
    registered_observer = observer;
}

void AptxAdModeMonitor_Enable(audio_driver_uc_instance_uid_t instance, const aptx_ad_mode_monitor_setup_t *params)
{
    DEBUG_LOG_FN_ENTRY("AptxAdModeMonitor_Enable: enum:audio_stack_use_cases_t:%d=0x%08X, enum:audio_source_t:%d", instance.use_case, instance.use_case, instance.instance);
    if (params->version == aptx_ad_mode_monitor_version_none)
    {
        DEBUG_LOG_WARN("AptxAdModeMonitor_Enable: Not aptX Adaptive, ignore");
        return;
    }
    if (state.is_enabled)
    {
        DEBUG_LOG_PANIC("AptxAdModeMonitor_Enable: Already enabled, currently supports only one instance");
    }

    aptxAdModeMonitor_ResetState();
    state.is_enabled = TRUE;
    state.use_case = instance;
    state.params = *params;
    AudioDriver_RegisterForEvent(aptxAdModeMonitor_AudioStackEventCallback, EVENT_SSRC_UPDATED_INDICATION);

    if (registered_observer && registered_observer->MonitorStartInd)
    {
        registered_observer->MonitorStartInd(&state.params);
    }
}

void AptxAdModeMonitor_Disable(audio_driver_uc_instance_uid_t instance)
{
    DEBUG_LOG_FN_ENTRY("AptxAdModeMonitor_Disable: enum:audio_stack_use_cases_t:%d=0x%08X, enum:audio_source_t:%d", instance.use_case, instance.use_case, instance.instance);
    if ((state.is_enabled == FALSE) || (IS_SAME_AUDIO_DRIVER_UC_INSTANCE_UID(state.use_case, instance) == FALSE))
    {
        DEBUG_LOG_WARN("AptxAdModeMonitor_Disable: Not current instance, ignore");
        return;
    }
    
    AudioDriver_DeregisterFromEvent(aptxAdModeMonitor_AudioStackEventCallback, EVENT_SSRC_UPDATED_INDICATION);
    aptxAdModeMonitor_ResetState();

    if (registered_observer && registered_observer->MonitorStopInd)
    {
        registered_observer->MonitorStopInd(&state.params);
    }
}